1714G - Path Prefixes - CodeForces Solution


binary search data structures dfs and similar trees *1700

Please click on ads to support us..

Python Code:

import sys,bisect,threading
input=sys.stdin.readline
sys.setrecursionlimit(3*(10**5))
def dfs(ans,a,b,v,tre,pb):
    for i in range(len(tre[v][0])):
        vtnxt=tre[v][0][i]
        x=tre[vtnxt][1]
        y=tre[vtnxt][2]
        a+=x
        b+=y
        pb.append(b)
        c=bisect.bisect_right(pb,a)
        ans[vtnxt-1]=c-1
        dfs(ans,a,b,vtnxt,tre,pb)
        a-=x
        b-=y
        pb.pop()
def main():
    for _ in range(int(input())):
        n=int(input())
                        tre={}
        for i in range(1,n+1):
            tre[i]=[[],0,0]
        for i in range(2,n+1):
            p,a,b=map(int,input().split())
            tre[p][0].append(i)
            tre[i][1]=a
            tre[i][2]=b
        ans=[0]*n
        a=0
        b=0
        pb=[0]
        dfs(ans,a,b,1,tre,pb)
        print(*ans[1:])
threading.stack_size(10 ** 8)
t = threading.Thread(target=main)
t.start()
t.join()

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ios ios::sync_with_stdio(0)
#define endl '\n'
#define int long long
#define ar array<int, 2>
#define arr array<int, 3>
const int N = 2e5 + 1000, M = 2 * N;
const int inf = 0x3f3f3f3f;
int mod = 998244353; //1e9+7;
int t, n, m, k;
int sum[N];
int a[N], b[N];
int ans[N];
map<int, vector<int>> mp;
void dfs(int u, int p, int d)
{
    sum[d] = sum[d - 1] + b[u];
    a[u] += a[p];
    int l = upper_bound(sum + 1, sum + d + 1, a[u]) - sum - 2;
    ans[u] = l;
    d++;
    for (int v : mp[u])
        dfs(v, u, d);
};
signed main()
{
    ios;
#ifdef DEBUG
    freopen("../1.in", "r", stdin);
#endif
    // 就是左边的固定长度。。有点最多到哪里。。还比左边的短。。或者等于。
    // 这题就是树上回溯+二分的一个运用题。。
    //
    cin >> t;
    while (t--)
    {
        mp.clear();
        cin >> n;
        for (int i = 2; i <= n; ++i)
        {
            // 他没有方向的。。草。。。麻烦。
            // you d ..
            int x;
            cin >> x >> a[i] >> b[i];
            mp[x].push_back(i);
        }
        dfs(1, 0, 1);
        for (int i = 2; i <= n; ++i)
            cout << ans[i] << ' ';
        cout << endl;
    }
};


Comments

Submit
0 Comments
More Questions

1523B - Lord of the Values
1406C - Link Cut Centroids
2409. Count Days Spent Together
2410. Maximum Matching of Players With Trainers
1604C - Di-visible Confusion
997A - Convert to Ones
218A - Mountain Scenery
486B - OR in Matrix
1405A - Permutation Forgery
1733A - Consecutive Sum
1733B - Rule of League
1733C - Parity Shuffle Sorting
1264A - Beautiful Regional Contest
1695A - Subrectangle Guess
467B - Fedor and New Game
252C - Points on Line
735C - Tennis Championship
992A - Nastya and an Array
554A - Kyoya and Photobooks
79B - Colorful Field
265B - Roadside Trees (Simplified Edition)
1362C - Johnny and Another Rating Drop
1214C - Bad Sequence
1091B - New Year and the Treasure Geolocation
244A - Dividing Orange
1061C - Multiplicity
1312A - Two Regular Polygons
801A - Vicious Keyboard
510B - Fox And Two Dots
616D - Longest k-Good Segment